package com.mangix.scheduler;

import com.mangix.conf.MangixConfig;
import com.mangix.core.MangixNode;

/**
* Created by Ahan on 2014/8/6.
*/
public class MangixStabilize extends Thread {



public MangixNode node;

    public MangixConfig config;

   public MangixStabilize(MangixNode node){
       this.node = node;
       this.config = node.getConfig();
       this.setName("Mangix Stabilize - " + node.toString());
   }

   @Override
   public void run() {
       int tick = 0;
       while (true) {
           tick = (tick + 1) % 2;
           try {
               switch (tick) {
                   case 0:
                       node.stabilize();
                       break;
                   case 1:
                       node.fixFingers();
                       break;
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
           try {
               long lastUpdateTime = this.node.getLastUpdateTime();
               long currentTime = System.currentTimeMillis();
               if (currentTime - lastUpdateTime > 1000 * 60) {
                   synchronized (this) {
                        this.wait(1000 * 60);
                   }
               } else {
                   Thread.sleep(2000);
               }
           } catch (Exception e) {
               e.printStackTrace();
           }
       }
   }
}